package mario.game.graymachine.sprite;

import javax.microedition.khronos.opengles.GL10;

import mario.game.graymachine.R;
import mario.game.graymachine.core.GConstant;
import mario.game.graymachine.core.Scene;
import mario.game.graymachine.core.OpenGLTestActivity.GameMsgHandler;
import android.os.Message;

public class Player extends GLSprite {
	
	private GLSprite grapSpri;
	private int hp;
	static final int STATE_BOMB=0;
	static final int ARRAY_LENGTH=10;
	static final int AUTO_BULLET_INERVAL=5;
	public Player(Scene scene){
		super(scene);
		mResourceIds= new int[5][ARRAY_LENGTH];
		mTextureNames=new int[5][ARRAY_LENGTH+2];
		hp=100;
		x=100;
		y=100;
		width=64;
		height=64;
		cwidth=8;
		cheight=8;
		// resouce id
		mTextureNames[1][ARRAY_LENGTH+1]=4;
		mResourceIds[1][0]=R.drawable.skate3;
		mResourceIds[1][1]=R.drawable.skate3;
		mResourceIds[1][2]=R.drawable.skate3_1;
		mResourceIds[1][3]=R.drawable.skate3_1;
		
		mTextureNames[2][ARRAY_LENGTH+1]=1;
		mResourceIds[2][0]=R.drawable.skate1;
		
		mTextureNames[4][ARRAY_LENGTH+1]=2;
		mResourceIds[4][0]=R.drawable.dead1;
		mResourceIds[4][1]=R.drawable.dead2;
		
		mTextureNames[0][ARRAY_LENGTH+1]=2;
		mResourceIds[0][0]=R.drawable.dead1;
		mResourceIds[0][1]=R.drawable.dead2;
	}
	
	@Override
	public void update(int timer) {
		super.update(timer);
		if(x<0&&velocityX<0){
			velocityX=0;
		}else if(x>480-64&&velocityX>0){
			velocityX=0;
		}
		if(y<0&&velocityY<0){
			velocityY=0;
		}else if(y>320-64&&velocityY>0){
			velocityY=0;
		}
		x+=timer*velocityX/100;
		y+=timer*velocityY/100;
		AnimaUpdate(timer);
		
		if(grapSpri!=null){
			updateGrapSpri();
		}
		
		
		if(moveTimesCount--==0){
			velocityX=0;
			velocityY=0;
		}
		
		
		//auto shot in here:
		if(autoShotNumber!=0){
			if(autoShotTimeInterval==0){
				autoShotNumber --;
				autoShotTimeInterval=AUTO_BULLET_INERVAL;
				attack();
			}
			autoShotTimeInterval --;
		}
	}
	private int autoShotNumber=0;
	private int autoShotTimeInterval;
	private void AnimaUpdate(int timer) {
		// TODO Auto-generated method stub
		if (mTextureNames[state][ARRAY_LENGTH] + timer >= mTextureNames[state][ARRAY_LENGTH+1] * 40)
			mTextureNames[state][ARRAY_LENGTH]= 0;
		mTextureNames[state][ARRAY_LENGTH] += timer;
		setTextureName(mTextureNames[state][mTextureNames[state][ARRAY_LENGTH] / 40]);
	}
	
	
	public void pushAttack(){
		autoShotNumber=3;
	}
	public void pushUp(){
		velocityY=8;
	}
	public void pushDown(){
		velocityY=-8;
	}
	public  void  pushLeft(){
		velocityX=-10;
	}
	public void pushRight(){
		velocityX=10;
	}

	public void releaseLeft() {
		velocityX=0;
	}

	public void releaseDown() {
		velocityY=0;
		
	}
	public void dead(){
		state=STATE_DEAD;
		isDead=true;
	}
	public boolean collisionWith(GLSprite spri) {
		switch (spri.type){
		case GLSprite.TYPE_ENIMY:
			Message msg = Message.obtain();
			msg.what=GConstant.PLAYER_UNDER_ATK;
			msg.obj=3;
			mGameMsgHandler.sendMessage(msg);
			hp-=3;
			if(hp<=0){
				dead();
				mGameMsgHandler.sendEmptyMessage(GConstant.PLAYER_DEAD);
			}
			break;
		case GLSprite.TYPE_SPRI_BULLET:
			spri.isDead=true;//destroy the bullet.
			msg = Message.obtain();
			msg.what=GConstant.PLAYER_UNDER_ATK;
			msg.obj=10;
			mGameMsgHandler.sendMessage(msg);
			hp-=10;
			if(hp<=0){
				dead();
				mGameMsgHandler.sendEmptyMessage(GConstant.PLAYER_DEAD);
			}
			break;
		case GLSprite.TYPE_POWERUP:
			if(spri.state!=STATE_DEAD){
				powerNumber++;
				Message m=Message.obtain();
				m.what=GConstant.INCR_PROGRESS;
				m.obj=powerNumber;
				mGameMsgHandler.sendMessage(m);
				spri.collisionWith(this);
				//sound;
			}
			break;
		case GLSprite.TYPE_STONE:
			
			break;
		default:
			break;
		}
		return false;
	}
	public void setup(){
		
	}
	int  powerNumber;
	int moveTimesCount;
	public GameMsgHandler mGameMsgHandler;
	public int powerType=2;
	public void setMoveTime() {
		moveTimesCount=7;
	}

	public void bomb() {
		
	}
	/**
	 * shot bullets
	 */
	public void attack(){
		mScene.addBullets(mScene.mSpritesRes[3], 40, 0, x+8, y+30, 32, 16, TYPE_PLAYER_BULLET);
//bullets sound
		Message msg = Message.obtain();
		msg.what=GConstant.SOUND;
		msg.obj=1;
		mGameMsgHandler.sendMessage(msg);
	}
	public void grap(GLSprite catchSpri) {
		grapSpri=catchSpri;
		
	}
	private void updateGrapSpri(){
		if(state==STATE_ATTACK){
			grapSpri.x=x+cwidth;
			grapSpri.y=y;
		}else{
			grapSpri.x=x-64;
			grapSpri.y=y-30;
		}
	}
	
	@Override
	public void draw(GL10 gl) {
		super.draw(gl);
		if(grapSpri!=null){
			grapSpri.draw(gl);
		}
	}
	
	@Override
	protected void detectExceedScreen() {
	}
}
